package com.midea.ai.air.ui.more;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.common.Constant;
import com.midea.ai.air.view.wheelview.ArrayWheelAdapter;
import com.midea.ai.air.view.wheelview.NumericWheelAdapter;
import com.midea.ai.air.view.wheelview.OnWheelScrollListener;
import com.midea.ai.air.view.wheelview.WheelView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hww on 2016/4/20.
 * 新建联动-启动条件-启动时间
 */
public class MoreSettingBrainpowerStartCondition_StartTime extends BaseActivity implements View.OnClickListener {


    private String result_one, result_two, result_three;
    private RelativeLayout rl_repetition;
    private TextView tv_Show;
    public static List<Boolean> mList_Selected = new ArrayList<>();//记录选择了星期几

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moresettingbrainpowerstartcondition_starttime);

        init();
        initWheelView();
    }

    private RelativeLayout bar_Rl;
    private LinearLayout back_left;


    private void init() {
        bar_Rl = (RelativeLayout) findViewById(R.id.bar_parent_rl);
        ViewGroup.LayoutParams LP = bar_Rl.getLayoutParams();
        LP.height = Constant.getTopMargin() + getResources().getDimensionPixelSize(R.dimen.height_44dp);
        bar_Rl.setLayoutParams(LP);

        back_left = (LinearLayout) findViewById(R.id.left_relativeLayout_back);
        back_left.setOnClickListener(this);


        tv_Show = (TextView) findViewById(R.id.more_setting_brainpower_start_condition_start_time_tv);
        rl_repetition = (RelativeLayout) findViewById(R.id.more_setting_brainpower_start_condition_start_time_rl);
        rl_repetition.setOnClickListener(this);


    }

    private WheelView am_pm, hour, mins;

    private void initWheelView() {
        am_pm = (WheelView) findViewById(R.id.am_pm);
        hour = (WheelView) findViewById(R.id.hour);
        mins = (WheelView) findViewById(R.id.mins);

        TextView tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        TextView tv_save = (TextView) findViewById(R.id.tv_save);
        tv_cancel.setOnClickListener(this);
        tv_save.setOnClickListener(this);

        final String dayString[] = new String[]{"上午", "下午"};
        am_pm.setAdapter(new ArrayWheelAdapter<>(dayString));

        hour.setCyclic(true);
        hour.setAdapter(new NumericWheelAdapter(1, 12));

        mins.setCyclic(true);
        mins.setAdapter(new NumericWheelAdapter(0, 59));

        am_pm.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                result_one = dayString[index];
                Log.i("hwwlog", "am_pm 结果：" + dayString[index]);
            }
        });

        hour.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                result_two = index + "";
                Log.i("hwwlog", "hour 结果：" + index);
            }
        });

        mins.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem() + 1;

                result_three = index + "";
                Log.i("hwwlog", "mins 结果：" + index);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mList_Selected.size() == 0) {
        } else {
            String mShow_Content = null;
            boolean show = false;
            for (int i = 0; i < mList_Selected.size(); i++) {
                if (mList_Selected.get(i)) {
                    if (!show) {
                        mShow_Content = getWeekContent(i);
                        show = true;
                    } else {
                        mShow_Content = mShow_Content + "  " + getWeekContent(i);
                    }
                }
            }
            if (show) {
                tv_Show.setText(mShow_Content);
            } else {
                tv_Show.setText("永不");
            }
            mList_Selected.clear();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mList_Selected.clear();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_relativeLayout_back:
                this.finish();
                break;
            case R.id.tv_cancel:

                break;
            case R.id.tv_save:
                Toast.makeText(this, "选择结果：" + result_one + result_two + "时" + result_three + "分", Toast.LENGTH_SHORT).show();
                break;

            case R.id.more_setting_brainpower_start_condition_start_time_rl:
                //重复
                startActivity(new Intent(this, MoreSettingBrainpowerStartCondition_StartTime_Repetition.class));
                break;
            default:
                break;
        }
    }


    private String getWeekContent(int i) {
        String content = null;
        switch (i) {
            case 0:
                content = "日";
                break;
            case 1:
                content = "一";
                break;
            case 2:
                content = "二";
                break;
            case 3:
                content = "三";
                break;
            case 4:
                content = "四";
                break;
            case 5:
                content = "五";
                break;
            case 6:
                content = "六";
                break;
            default:
                break;
        }
        return content;
    }

}
